#include "Enemy.h"


CEnemy::CEnemy(void)
{
	//this->sprite->_texture = new CTexture(ENEMY_FILE, 11, 2, 22);
	this->sprite = new CSprite(new CTexture(ENEMY_FILE, 11, 2, 22), 100);
	this->_Vx = 10;
	this->Gravity = 200;
	this->left = false;
	this->Alive = true;
	//this->sprite->_index = 13;
	this->lastAnimation = 0;
}


CEnemy::~CEnemy(void)
{
}

void CEnemy::Animation(CDynamicObjects* obj)
{
	if (obj->Die)
	{
		if(obj->left)
		{
			obj->CurFrame = 10;
		}
		else
		{
			obj->CurFrame = 11;
		}
		return;//ko co animation
	}
	if (timeGetTime() >= obj->lastAnimation + 100)
	{
		obj->lastAnimation = timeGetTime();
		//Goomba
		if(obj->iKind == 1 && obj->iType == 0)
		{
			if (obj->State == 1)//die
			{
				return;
			}
			else
			{
				if (obj->left)//di chuyen sang trai
				{
					obj->CurFrame++;
					if (obj->CurFrame > 9)
					{
						obj->CurFrame =1;
					}
				}
				else
				{
					obj->CurFrame--;
					if (obj->CurFrame < 12)
					{
						obj->CurFrame = 20;
					}
				}
			}
		}

	}
	this->sprite->SelectIndex(obj->CurFrame);
}


void CEnemy::Render(CObjects *obj)
{
	int x = obj->Pos.x - G_ScreenRect.left;
	int y = obj->Pos.y - G_ScreenRect.top;
	sprite->Draw(x, y);
}

void CEnemy::MoveX(CDynamicObjects* obj,int  delta)
{
	float v = (_Vx*(float)delta)/1000;
	if (obj->left)
	{
		obj->Pos.x -= v;
	}
	else
	{
		obj->Pos.x += v;
	}
	obj->RectArea.left = obj->Pos.x;
	obj->RectArea.right = obj->Pos.x + 50;
	obj->RectArea.top = obj->Pos.y;
	obj->RectArea.bottom = obj->Pos.y + 50;
	obj->RectArea.top += 10;
}

void CEnemy::MoveY(CDynamicObjects* obj,int  delta)
{
	obj->Pos.y += (Gravity*delta)/1000;
	obj->RectArea.left = obj->Pos.x;
	obj->RectArea.right = obj->Pos.x + 50;
	obj->RectArea.top = obj->Pos.y;
	obj->RectArea.bottom = obj->Pos.y + 50;
	obj->RectArea.top += 10;
}

bool CEnemy::CheckMoveY(CDynamicObjects* obj,int delta,CNode *_NodeRoot)
{
	CObjects *newObj;
	D3DXVECTOR3 Oldpos = obj->Pos;
	newObj = obj;
	MoveY(obj,delta);
	if (_NodeRoot->CheckCollision(newObj->RectArea))// t va cham
	{
		obj->Pos = Oldpos;
		obj->RectArea.left = Oldpos.x;
		obj->RectArea.right = Oldpos.x + 50;
		obj->RectArea.top = Oldpos.y;
		obj->RectArea.bottom = Oldpos.y + 50;
		obj->RectArea.top += 10;
		MoveY(obj,delta*3/4);
		if (_NodeRoot->CheckCollision(newObj->RectArea))//3/4 t va cham
		{
			obj->Pos = Oldpos;
			obj->RectArea.left = Oldpos.x;
			obj->RectArea.right = Oldpos.x + 50;
			obj->RectArea.top = Oldpos.y;
			obj->RectArea.bottom = Oldpos.y + 50;
			obj->RectArea.top += 10;
			MoveY(obj,delta/2);
			if (_NodeRoot->CheckCollision(newObj->RectArea))// t/2 va cham
			{
				obj->Pos = Oldpos;
				obj->RectArea.left = Oldpos.x;
				obj->RectArea.right = Oldpos.x + 50;
				obj->RectArea.top = Oldpos.y;
				obj->RectArea.bottom = Oldpos.y + 50;
				obj->RectArea.top += 10;
				MoveY(obj,delta/4);
				if (_NodeRoot->CheckCollision(newObj->RectArea))//t/4 va cham
				{
					obj->Pos = Oldpos;
					obj->RectArea.left = Oldpos.x;
					obj->RectArea.right = Oldpos.x + 50;
					obj->RectArea.top = Oldpos.y;
					obj->RectArea.bottom = Oldpos.y + 50;
					obj->RectArea.top += 10;
					return true;//co va cham Y--> moveX
				}
				else
				{
					_NodeRoot->Insert(newObj);
					return false;
				}
			}
			else
			{
				_NodeRoot->Insert(newObj);
				return false;
			}
		}
		else
		{
			_NodeRoot->Insert(newObj);
			return false;
		}
	}
	else//ko va cham
	{
		//move y
		//dua vao cay
		_NodeRoot->Insert(newObj);
		return false;
	}



}

bool CEnemy::CheckMoveX(CDynamicObjects* obj,int delta,CNode *_NodeRoot)
{

	CObjects *newObj;
	newObj = obj;
	D3DXVECTOR3 Oldpos = obj->Pos;
	MoveX(obj,delta);
	if (_NodeRoot->CheckCollision(newObj->RectArea))// t va cham
	{
		obj->Pos = Oldpos;
		obj->RectArea.left = Oldpos.x;
		obj->RectArea.right = Oldpos.x + 50;
		obj->RectArea.top = Oldpos.y;
		obj->RectArea.bottom = Oldpos.y + 50;
		obj->RectArea.top += 10;
		MoveX(obj,delta*3/4);
		if (_NodeRoot->CheckCollision(newObj->RectArea))//3/4 t va cham
		{
			obj->Pos = Oldpos;
			obj->RectArea.left = Oldpos.x;
			obj->RectArea.right = Oldpos.x + 50;
			obj->RectArea.top = Oldpos.y;
			obj->RectArea.bottom = Oldpos.y + 50;
			obj->RectArea.top += 10;
			MoveX(obj,delta/2);
			if (_NodeRoot->CheckCollision(newObj->RectArea))// t/2 va cham
			{
				obj->Pos = Oldpos;
				obj->RectArea.left = Oldpos.x;
				obj->RectArea.right = Oldpos.x + 50;
				obj->RectArea.top = Oldpos.y;
				obj->RectArea.bottom = Oldpos.y + 50;
				obj->RectArea.top += 10;
				MoveX(obj,delta/4);
				if (_NodeRoot->CheckCollision(newObj->RectArea))//t/4 va cham
				{
					return true;//co va cham Y--> moveX
				}
				else
				{
					_NodeRoot->Insert(newObj);
					return false;
				}
			}
			else
			{
				_NodeRoot->Insert(newObj);
				return false;
			}
		}
		else
		{
			_NodeRoot->Insert(newObj);
			return false;
		}
	}
	else//ko va cham
	{
		//move y
		//dua vao cay
		_NodeRoot->Insert(newObj);
		return false;
	}

}

void CEnemy::Update(CObjects *obj,int Delta, CNode *_NodeRoot)
{
	D3DXVECTOR3 Oldpos;
	CDynamicObjects *Obj = (CDynamicObjects*)(obj);
	if (obj->Die == false)//chua chet
	{
		Oldpos= obj->Pos;
		this->Animation(Obj);
		if (this->CheckMoveY(Obj,Delta,_NodeRoot) == true)// Ko di chuyen y
		{
			if(this->CheckMoveX(Obj,Delta,_NodeRoot)==true)//Ko di chuyen  X
			{
				//doi huong du chuyen
				Obj->left = !Obj->left;
				Obj->Pos = Oldpos;
				Obj->RectArea.left = Oldpos.x;
				Obj->RectArea.right = Oldpos.x + 50;
				Obj->RectArea.top = Oldpos.y;
				Obj->RectArea.bottom = Oldpos.y + 50;
				Obj->RectArea.top += 10;
				//_StaticObjects->push_back(obj);
			}
		}
	}
	else//chet
	{
		this->Animation(Obj);
	}

}